define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'shop/task/task/index',
                    add_url: 'shop/task/task/add',
                    edit_url: 'shop/task/task/edit',
                    del_url: 'shop/task/task/del',
                    multi_url: 'shop/task/task/multi',
                    table: 'shop_task',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'task_id',
                sortName: 'task_id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'task_sno', title: __('Task_sno')},
                        {field: 'send_time', title: __('Send_time'), operate:false, addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'shop_admin_id', title: __('Admin_id'),operate:false},
                        {field: 'username', title: __('Admin_username'),operate:false},
                        {field: 'shop_name', title: __('Shop_name')},
                        {field: 'key_word', title: __('key_word')},
                        {field: 'task_type', title: __('Task_type'),searchList: {
                            "1":__('精刷单'),
                            "2":__('隔日单'),
                            "3":__('流量单')
                        },formatter:function(value,row,index){
                                var keys = typeof this.searchList === 'object' ? Object.keys(this.searchList) : [];
                                var index = keys.indexOf(value.toString());
                                value = value === null ? '' : value.toString();
                                var display = index > -1 ? this.searchList[value] : null;
                                var icon = typeof this.icon !== 'undefined' ? this.icon : null;
                                if (!display) {
                                    display = __(value.charAt(0).toUpperCase() + value.slice(1));
                                }
                                var html = '<span class="text">' + (icon ? '<i class="' + icon + '"></i> ' : '') + display + '</span>';
                                return html;
                            }},
                        {field: 'beat_price', title: __('Cost_price'),operate:false},
                        {field: 'order_amount', title: __('佣金'),operate:false},
                        {field: 'goods_sno', title: __('Goods_sno'), operate:false},
                        {field: 'total_task_num', title: __('Total_task_num'),operate:false},
                        {field: 'task_use_num', title: __('Task_use_num'),operate:false},
                        {field: 'left_task_num', title: __('Left_task_num'),operate:false},

                        {field: 'task_status', title: __('Task_status'),searchList: {
                            "1":__('未发布'),
                            "2":__('已发布'),
                        },formatter:function(value,row,index){
                                var keys = typeof this.searchList === 'object' ? Object.keys(this.searchList) : [];
                                var index = keys.indexOf(value.toString());
                                value = value === null ? '' : value.toString();
                                var display = index > -1 ? this.searchList[value] : null;
                                var icon = typeof this.icon !== 'undefined' ? this.icon : null;
                                if (!display) {
                                    display = __(value.charAt(0).toUpperCase() + value.slice(1));
                                }
                                var html = '<span class="text">' + (icon ? '<i class="' + icon + '"></i> ' : '') + display + '</span>';

                                return html;
                            }},
                        {field: 'sd_shop_task.create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'update_time', title: __('Update_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'delete_time', title: __('Delete_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'operate', title: __('Operate'), table: table,  buttons: [
                        //         {name: 'name2', text: '发布任务', title: '发布任务', icon: 'fa fa-flash', classname: 'btn btn-xs btn-success btn-ajax', url: 'shop/task/lists/send', success:function(data, ret){
                        //             if(ret.code == 1){
                        //                 data[ids].task_status = '已发布'
                        //                 console.log(data[ids]);
                        //             }
                        //         }, error:function(){}},
                        //     ], events: Table.api.events.operate, formatter: Table.api.formatter.operate,formatter: function (value, row, index) {
                        //         console.log(row);
                        //         $(table).data("operate-edit", null);
                        //         return Table.api.formatter.operate.call(this, value, row, index);
                        //     }}
                    ]
                ]
            });

            // $(document).on("click", "#add", function (e){
            //     var data = table.bootstrapTable('getSelections');
            //     var post_data = JSON.stringify(data);
            //     var data = table.bootstrapTable('getSelections');
            //     var post_data = JSON.stringify(data);
            //     var btn_array = ['确定', '保存草稿'];
            //     var btn_evn_array = {
            //         evn_1:function () {
            //             console.log(JSON.stringify(table.bootstrapTable('getSelections')));
            //             $("#type").val('send');
            //             $("#add-form").submit();
            //             return false;
            //         },
            //         evn_2:function () {
            //             console.log(2);
            //              $("#type").val('draft');
            //             $("#add-form").submit();
            //             return false;
            //         }
            //     };
            //     Fast.api.new_open("shop/task/lists/add?data="+post_data, __('添加任务'),btn_array,btn_evn_array,2);
            // });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            $(document).on("click","#draft",function(){
                $("#type").val('draft');
                $("#add-form").submit();
            });

            $(document).on("click","#next_1",function(){
                $("#shop_choose").removeClass('active');
                $("#tab_1").removeClass('active');
                $("#module").addClass('active');
                $("#tab_2").addClass('active');
            });
            $(document).on("click","#last_1",function(){
                $("#shop_choose").addClass('active');
                $("#tab_1").addClass('active');
                $("#module").removeClass('active');
                $("#tab_2").removeClass('active');
            });

            $(document).on("click","#next_2",function(){
                $("#module").removeClass('active');
                $("#tab_2").removeClass('active');
                $("#others").addClass('active');
                $("#tab_3").addClass('active');
            });

            $(document).on("click","#last_2",function(){
                $("#module").addClass('active');
                $("#tab_2").addClass('active');
                $("#others").removeClass('active');
                $("#tab_3").removeClass('active');
            });

            $(document).on("click","#ok",function(){
                $("#type").val('send');
                $("#add-form").submit();
            });

            var html = '';
            $(document).on("click","#add_key",function(){
                var len = $("#key_word").children().length;
                //console.log(len)
                if(len > 10){
                    alert('最多只能添加十个关键字');
                    return false;
                }
                len++;
                html = '<div id="key_'+len+'">\
                            <div class="col-xs-12 col-sm-6">\
                                <div class="form-group">\
                                    <label class="control-label col-xs-12 col-sm-4"></label>\
                                    <div class="col-xs-12 col-sm-4">\
                                        <input id="" data-rule="required" class="form-control form-control" name="row[key_word][]" type="text">\
                                    </div>\
                                </div>\
                            </div>\
                            <div class="col-xs-12 col-sm-6">\
                                <div class="form-group">\
                                    <label class="control-label col-xs-12 col-sm-4"></label>\
                                    <div class="col-xs-12 col-sm-4">\
                                        <input id="" data-rule="required" class="form-control form-control" name="row[task_num][]" type="num">\
                                    </div>\
                                </div>\
                            </div>\
                        </div>';
                $("#key_word").append(html)
            });

            $(document).on("click","#del_key",function(){
                var len = $("#key_word").children().length;
                if(len < 2){
                    alert('至少要保留一个关键字');
                    return false;
                }
                $("#key_"+len).remove();
                len--;
            });
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },

        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});